<?php

if (strpos ($_SERVER['PHP_SELF'], 'functions_autotag.inc') !== false) {
    die ('This file can not be used on its own.');
}

//=====自動タグ=================================================================
function plugin_autotags_assist (
    $op
    , $content = ''
    , $autotag = ''
)
// +---------------------------------------------------------------------------+
// | 自動タグ
// | 書式 plugin_autotags_assist ($op, $content, $autotag)
// +---------------------------------------------------------------------------+
// 2010/08/03
{
    global $_TABLES;
    global $_CONF;
    global $_USER;
	
//    global $_PLUGINS;

    global $LANG_ASSIST;
    global  $topic;
//    global  $page;

    global $LANG_ASSIST_autotag_desc;
	global $_GROUPS;
	
	$tagname = array();
    $tagname[]='newstories';
    $tagname[]='newstories2';
    $tagname[]='conf';
    $tagname[]='assist';
	
	//Option Display
    if ($op == 'tagname' ) {
		return $tagname;
	} elseif ($op == 'permission' || $op == 'nopermission') {
	    if ($op == 'permission') {
            $flag = true;
        } else {
            $flag = false;
        }

        if (isset($_GROUPS['Assist Admin'])) {
            $group_id = $_GROUPS['Assist Admin'];
        } else {
            $group_id = DB_getItem($_TABLES['groups'], 'grp_id',
                                   "grp_name = 'Assist Admin'");
        }
        $owner_id = SEC_getDefaultRootUser();

		if (COM_getPermTag(
			$owner_id
			, $group_id
			, $_ASSIST_CONF['autotag_permissions_assist'][0]
			, $_ASSIST_CONF['autotag_permissions_assist'][1]
			, $_ASSIST_CONF['autotag_permissions_assist'][2]
			, $_ASSIST_CONF['autotag_permissions_assist'][3]) == $flag) {
            return $tagname;
        }
    } elseif ($op == 'description') {
        return $LANG_ASSIST_autotag_desc;
		
	} else if ($op == 'parse') {
        //引数チェック
        $p1 = COM_applyFilter ($autotag['parm1']);
        $p2 = COM_applyFilter ($autotag['parm2']);
        $tag = COM_applyFilter ($autotag['tag']);
        $tagstr = COM_applyFilter ($autotag['tagstr']);

        //各処理
        switch( $tag ) {
            //----------------
            //新着記事（おしらせ）
            //[newstories:話題ID RSSファイル]
            case 'newstories' :
                $rt= phpblock_shownewstories($p1,$p2);
                $content = str_replace ($autotag['tagstr'], $rt, $content);
                return $content;
                break;
            case 'newstories2' :
                $p=assist_parm2($p2);

                $rt= phpblock_shownewstories($p1
                        ,$p['rss_file']
                        ,$p['title_trim_length']
                        ,$p['intervalday']
                        ,$p['limitcnt']
                        ,$p['newmarkday']
                        ,$p['thtml']
                        );
                $content = str_replace ($autotag['tagstr'], $rt, $content);
                return $content;
                break;
            case 'conf' :
                $rt=$_CONF[$p1];
                $content = str_replace ($autotag['tagstr'], $rt, $content);
                return $content;
                break;
            case 'assist' :
                $p=assist_parm2($p2);
                switch($p1) {
                    case 'usercount':
                        $rt=DB_getItem($_TABLES['userinfo']
                            ,"count(uid)","uid<>1");
                        $rt=COM_NumberFormat($rt);
                        break;
                    case 'usercount_location':
                        $rt=assist_usercount_location();
                        break;
                    case 'staticpage_content':
                        $rt=phpblock_showstaticpage(
                             $p['id']
                            ,$p['urlkey']
                            ,$p['mode']
                            );
                        break;
                    case 'user_homepage':
                        $rt=assist_returnUser(
                             "homepage"
                            ,$p['uid']
                            );
                        break;
                    case 'user_location':
                        $rt=assist_returnUser(
                             "location"
                            ,$p['uid']
                            );
                        break;
                    case 'user_sig':
                        $rt=assist_returnUser(
                             "sig"
                            ,$p['uid']
                            );
                        break;
                    case 'user_photo':
                        $width=$p['width'];
                        $height=$p['height'];
                        $rt=assist_returnUser(
                             "photo"
                            ,$p['uid']
                            ,$p['width']
                            ,$p['height']
                            );
                        break;
                    case 'user_about':
                        $rt=assist_returnUser(
                             "about"
                            ,$p['uid']
                            );
                        break;
                    case 'user_pgpkey':
                        $rt=assist_returnUser(
                             "pgpkey"
                            ,$p['uid']
                            );
                        break;

                    case 'uid':
                        $rt=$_USER['uid'];
                    break;

                    case 'username':
                    case 'fullname':
                        $rt=assist_returnUser(
                             $p1
                            ,$p['uid']
                            );
                        break;
                    case 'showgroup':
                        $rt=phpblock_showgroup();
                        break;
                    case 'newstories' :
                        $rt= phpblock_shownewstories(
                            $p['topics']
                            ,$p['rss_file']
                            ,$p['title_trim_length']
                            ,$p['intervalday']
                            ,$p['limitcnt']
                            ,$p['newmarkday']
                            ,$p['templatedir']
                            ,true
                        );
                        break;
					case 'rss' :
                        $rt= assist_getrss(
                            $p['urlrss']
                            ,$p['description_trim_length']
                            ,$p['strip_tags']
                            ,$p['templatedir']
                        );
                        break;
                    //
                    case 'home_id' :
                        if( COM_onFrontpage() ){
                            $rt=$LANG_ASSIST['home_id']['home'];
                        } else {
                            $rt=$LANG_ASSIST['home_id']['sub'];
                        }
                        break;
                    case 'login_status' :
                        if  (COM_isAnonUser()){
                            $rt=$LANG_ASSIST[$p1][0];
                        } else {
                            $rt=$LANG_ASSIST[$p1][1];
                        }
                        break;
                    case 'login_withfacebook' :
						$rt=assist_loginform(
							'facebook'
							,$p['logoutform']);
                        break;
                    case 'topic_id' :
                    case 'sp_id' :
                        $rt=assist_arg($p1);
                        break;
                    case 'btn' :
                        $rt= assist_btn(
                            $p['type']
                            ,$p['templatedir']
                        );
                        break;
                    case 'trancate' :
                        $rt= assist_truncate(
                            $p['text']
                            ,$p['maxlen']
                            ,$p['filler']
                            ,$p['endchars']
                        );
                        break;

                }

                $content = str_replace ($autotag['tagstr'], $rt, $content);
                return $content;
                break;

        }
    }
}

//第二引数分割
function assist_parm2 ($p2)
{
    $ary=array();
    $px = explode (' ', trim ($p2));
    if (is_array ($px)) {
        foreach ($px as $part) {
            $a = explode (':', $part);
            $ary["{$a[0]}"]=$a[1];
        }
    }
    return $ary;

}

//=====ブロック関数============================================================
function phpblock_showgroup()
// +---------------------------------------------------------------------------+
// | 機能  ブロック用 ログインユーザの所属するグループ一覧出力
// | 書式 phpblock_showgroup()
// +---------------------------------------------------------------------------+
// | 戻値 nomal:ログインユーザの所属するグループ一覧
// +---------------------------------------------------------------------------+
//2007/09/03 15:11 tsuchi AT geeklog DOT jp http://www.geeklog.jp/
{

    global $_USER,$_TABLES;

    $retval="";

    if (!empty ($_USER['uid']) && ($_USER['uid'] > 1)) {
        $sql="SELECT ";
        $sql.=" grp_name ,grp_descr ";
        $sql.=" FROM {$_TABLES['group_assignments']}";
        $sql.=" ,{$_TABLES['groups']} ";
        $sql.=" WHERE ug_main_grp_id = grp_id ";
        $sql.=" and UG_UID=".$_USER['uid'];
        $sql.=" and grp_name<>'Logged-in Users'";
        $sql.=" and grp_name<>'All Users'";
        $sql.=" ORDER BY ug_main_grp_id";

        $result = DB_query ($sql);

        while( $A = DB_fetchArray( $result ) )    {
            $retval .= "<li>";
            $retval .="<div title='{$A['grp_descr']}'>";
            $retval .= $A['grp_name'];
            $retval .="</div>";
            $retval .= "</li>";
            $retval .= LB;
        }
    }
    return $retval;
}

function phpblock_shownewstories(
     $topics=""
    ,$rss_file=""
    ,$title_trim_length=""
    ,$intervalday=""
    ,$limitcnt=""
    ,$newmarkday=""
    ,$thtml=""
    ,$templatePath=false
)
// +---------------------------------------------------------------------------+
// | 機能  ブロック用 新着記事一覧出力                                         |
// | 書式 phpblock_shownewstories                                              |
// | 書式 phpblock_shownewstories("news","news.rss")                           |
// | 書式 phpblock_shownewstories("news","news.rss",20,90,10,3)                |
// | 書式 phpblock_shownewstories("ALL")                                       |
// | 書式 phpblock_shownewstories("news,geeklog")                              |
// +---------------------------------------------------------------------------+
// | 引数 $topic         :選択トピック 省略時設定値or'ALL'                     |
// | 引数 $rss_file      :RSS_file     省略時""                                |
// | 引数 $title_trim_length :タイトル長の制限 省略時設定値or20                |
// | 引数 $intervalday   :新着記事の期間 単位日  省略時設定値or90              |
// |                      0の時　全件                                          |
// | 引数 $limitcnt      :表示件数   省略時設定値or10                          |
// | 引数 $newmarkday    :新着マーク 省略時設定値or3                           |
// | 引数 $newmarkday    :新着マーク 省略時設定値or3                           |
// | 引数 $thtml         :テンプレートファイルのフォルダ  layout/テーマ/XXXX/  |
// |                      省略時は、プラグイン内のテンプレート                 |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:新着記事一覧                                                   |
// +---------------------------------------------------------------------------+
// 2009/04/02　引数 $thtml追加
// 2009/04/02　テンプレート変数 class 追加
// 2009/04/02　$intervalday=0の時全件
{

    global $_USER;
    global $_TABLES;
    global $_CONF;
    global $_ASSIST_CONF;
    global $LANG_ASSIST;

    //
    if ($title_trim_length==""){
        $title_trim_length=$_ASSIST_CONF['title_trim_length'];
        if ($title_trim_length==""){
            $title_trim_length=20;
        }
    }
    //
    if ($intervalday==""){
        $intervalday=$_ASSIST_CONF['intervalday'];
        if ($intervalday==""){
            $intervalday=90;
        }
    }
    //
    if ($limitcnt==""){
        $limitcnt=$_ASSIST_CONF['limitcnt'];
        if ($limitcnt==""){
            $limitcnt=10;
        }
    }
    if ($newmarkday==""){
        $newmarkday=$_ASSIST_CONF['newmarkday'];
        if ($newmarkday==""){
            $newmarkday=3;
        }
    }

    if ($topics==""){
        $topics=$_ASSIST_CONF['topics'];
        if ($topics==""){
            $topics="'ALL'";
        }
    }elseif (strpos($topics,",")=="") {
        $topics="'$topics'";
    }else{
        $ary=explode(",",$topics);
        $b="";
        for ($i = 1; $i <= count($ary); $i++) {
            $b.="'".current($ary)."',";
            next($ary);
        }
        $topics=rtrim($b,",");
    }

    //
    $new_img=$_ASSIST_CONF['new_img'];
    if ($new_img==""){
        $new_img="New!";
    }
    $rss_img=$_ASSIST_CONF['rss_img'];
    if ($rss_img=="") {
        $rss_img="[RSS]";
    }

//-------------------------------------------
    $retval="";

    $pi_name="assist";


    if ($templatePath){
        //テンプレートフォルダの設定
        $tmplfld=assist_templatePath('newstories',$thtml,$pi_name);
        $list = new Template($tmplfld);

    }else{
        if ($thtml<>""){
            if (file_exists($_CONF['path_layout'] . "/".$thtml)) {
                $list = new Template ($_CONF['path_layout']. "/".$thtml);
            }else{
                $list = new Template ($_CONF['path'] . "plugins/assist/templates/");
            }
        } else {
            $list = new Template ($_CONF['path'] . "plugins/assist/templates/");
        }
    }

    //
    if ($title_trim_length==""){
        $title_trim_length=$_ASSIST_CONF['title_trim_length'];
        if ($title_trim_length==""){
            $title_trim_length=20;
        }
    }
    //
    if ($intervalday==""){
        $intervalday=$_ASSIST_CONF['intervalday'];
        if ($intervalday==""){
            $intervalday=90;
        }
    }


    $list->set_file (array (
        'list' => 'stories.thtml',
        'col'   => 'stories_col.thtml'
        ));


    $sql="SELECT ";
    $sql.=" s.title";
    $sql.=" , UNIX_TIMESTAMP(s.date) AS day";
    $sql.=" , t.topic";
    $sql.=" , s.tid";
    $sql.=" , s.sid";

    $sql.=" FROM ";
    $sql.=" {$_TABLES['stories']} AS s";
    $sql.=" ,{$_TABLES['topics']} AS t";

    $sql.=" WHERE";
    $sql.="  s.tid = t.tid";
    $sql.=" AND t.archive_flag=0 ";
    $sql.=" AND s.draft_flag=0 ";
    $sql.=" AND date <= NOW() ";

    if ($intervalday<>0){
        $sql.=" AND (date >= (date_sub(NOW(), INTERVAL $intervalday day)))";
    }

    if ($topics!=="'ALL'" ){
        $sql.=" AND s.tid IN ($topics)";
    }
    $sql .= COM_getTopicSql ('AND', 0, 's');
    $sql .= COM_getPermSql ('AND', 0, 2, 's' );
    $sql .= COM_getLangSQL ('sid', 'AND');

    $sql.=" ORDER BY date DESC";

    $sql.=" LIMIT $limitcnt";

    $result = DB_query ($sql);

    $i=0;
    while( $A = DB_fetchArray( $result ) )    {

        $url=COM_buildUrl($_CONF['site_url'] . '/article.php?story=' . $A['sid']);
        $title= COM_truncate( $A['title'], $title_trim_length,'...' );

        $list->set_var ('site_url', $_CONF['site_url']);
        $list->set_var ('day', strftime($LANG_ASSIST[2],$A['day']));

        $chkday=strtotime("- $newmarkday days",time());
        if (date("Ymd",$A['day']) >= date("Ymd",$chkday)){
            $list->set_var ('new_img', $new_img);
        }else{
            $list->set_var ('new_img', '');
        }

        $list->set_var ('url', $url);
        $list->set_var ('title', $title);
        $n=($i%2)+1;
        $class='class="row'.$n.'"';
        $list->set_var ('class', 'class="row'.$n.'"');

        $list->parse ('stories_col', 'col', true);

        $i++;
    }

    $list->set_var ('site_url', $_CONF['site_url']);
    //
    if ($rss_file<>""){
        $list->set_var ('rss_url', $_CONF['site_url'] . '/backend/'.$rss_file);
        $list->set_var ('rss_title', $rss_img);
    }
    //
    $more_title=$LANG_ASSIST[1];
    if ($topics=="ALL" OR strpos($topics,",")<>"") {
        $more_url=COM_buildUrl($_CONF['site_url'] . '/directory.php');
    }else{
        //$more_url=COM_buildUrl($_CONF['site_url'] . '/index.php?topic=' . $topics);
        $more_url=$_CONF['site_url'] . '/index.php?topic=' . ltrim(rtrim($topics,"'"),"'");
    }
    $list->set_var ('more_url', $more_url);
    $list->set_var ('more_title', $more_title);

    $list->parse ('output', 'list');
    $retval .= $list->finish ($list->get_var ('output'));

    return $retval;
}
//-----
function phpblock_shownewstories_news(
)
{
    return phpblock_shownewstories("news","news.rss");
}


function phpblock_showstaticpage(
     $id="blockpage"
	,$urlkey=""
    ,$mode=""
)
// +---------------------------------------------------------------------------+
// | 機能  ブロック用 静的ページ出力
// | 書式 phpblock_showstaticpage
// | 書式 phpblock_showstaticpage($id)
// | 書式 phpblock_showstaticpage($id,"yyyy")
// | 書式 phpblock_showstaticpage($id,"","home")
// | 書式 phpblock_showstaticpage($id,"","sub")
// +---------------------------------------------------------------------------+
// | 引数 $id         :静的ページID
// | 引数 $urlkey     :URLにkeywordが含まれた場合に表示する
// |                   省略時は、無条件
// | 引数 $mode       :home,sub それ以外はall
// +---------------------------------------------------------------------------+
// | 戻値 nomal:静的ページの内容                                               |
// +---------------------------------------------------------------------------+
// 2011/11/14
{
	global $_CONF;

    $retval ='';

    if ($id==""){
         $id="blockpage";
	}
	$mode=strtoupper($mode);
	
	if ($mode=="HOME"){
		if  (COM_onFrontpage()){
			$retval .= assist_returnStaticpage( $id ,"autotag");
		}
	}elseif ($mode=="SUB"){
		if  (!COM_onFrontpage()){
			$retval .= assist_returnStaticpage( $id ,"autotag");
		}
	}else{
		if ($urlkey==""){
			$retval .= assist_returnStaticpage( $id ,"autotag");
		}else{
			$pageurl = $_SERVER["REQUEST_URI"];
			if( strpos($pageurl, $urlkey) !== FALSE ){
				$retval .= assist_returnStaticpage( $id ,"autotag");
			}
		}
	}
    return $retval;
}

//=====専用関数============================================================
//
function assist_getrss(
    $urlrss=''
	,$description_trim_length=0
	,$strip_tags=-1
    ,$thtml=""
)
{

	global $_CONF;
	
	$pi_name="assist";


	if  ($urlrss==""){
		$urlrss=$_CONF["site_url"]."/backend/geeklog.rss";
	}else{
		$urlrss="http://".$urlrss;
	}
	
	
    //テンプレートフォルダの設定
    $tmplfld=assist_templatePath('rss',$thtml,$pi_name);
    $list = new Template($tmplfld);

    $list->set_file (array (
        'list' => 'rss.thtml',
        'col'   => 'rss_col.thtml'
        ));
	
//echo "urlrss=".$urlrss."<br>";
	$retval = '';
	$values=assist_getfeedvalues($urlrss);
	
	$retval.="<ul>";
	foreach($values as $value){
		$tag=$value["tag"];
		$tag=strtolower($tag);
		$type=$value["type"];
		$level=$value["level"];
		$value=$value["value"];
//echo "tag=".$tag." type=".$type." level=".$level." value=".$value."<br>";		
		if  ($type=="open"){
			$pubdate="";
			$title="";
			$description="";
			$link="";
		}elseif  ($type=="close"){
			if ($tag=="item"){
				$list->set_var ('link', $link);
				$list->set_var ('title', $title);
				$list->set_var ('pubdate', $pubdate);
				$list->set_var ('description', $description);
		        $list->parse ('rss_col', 'col', true);

			}elseif  ($tag=="image"){
				$list->set_var ('image_link', $link);
				$list->set_var ('image_title', $title);
				$list->set_var ('image_url', $url);
			}
		}elseif  ($type=="complete" AND $level==4){
			switch($tag){
				case "title":
					$title=$value;
					break;
				case "link":
					$link=$value;
					break;
				case "url":
					$url=$value;
					break;
				case "description":
					if ($strip_tags){
						$value=strip_tags($value);
						if	($description_trim_length<>0 
							AND mb_strlen($value)>$description_trim_length){
							$value=mb_substr(
							$value,0,$description_trim_length)."...";
						}
					}
					$description=$value;
					break;
				case "pubdate":
					$dateary=COM_getUserDateTimeFormat($value);
					$pubdate=$dateary['0'];
					break;
			}
		}elseif  ($type=="complete" AND $level==3){
			switch($tag){
				case "title":
					$list->set_var ('head_link', $link);
					break;
				case "title":
					$list->set_var ('head_title', $value);
					break;
				case "description":
					$list->set_var ('head_description', $value);
					break;
				case "webmaster":
					$list->set_var ('head_webmaster', $value);
					break;
				case "copyright":
					$list->set_var ('head_copyright', $value);
					break;
				case "pubdate":
					$dateary=COM_getUserDateTimeFormat($value);
					$list->set_var ('head_copyright', $dateary['0']);
					break;
			}
		}
	}
    $list->parse ('output', 'list');
    $retval .= $list->finish ($list->get_var ('output'));
	
	return $retval;

}


function assist_getfeedvalues(
	$url
)
{

	global $_CONF;
    require_once $_CONF['path_system']
                 . '/classes/syndication/parserfactory.class.php';
    require_once $_CONF['path_system']
                 . '/classes/syndication/feedparserbase.class.php';

    $factory = new FeedParserFactory($_CONF['path_system']
                                     . '/classes/syndication/');
	$content = $factory->_getFeed($url);
	
    // XMLデータを配列に格納
	$xml_parser=xml_parser_create();
	xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
    $rt=xml_parse_into_struct($xml_parser,$content,$values,$idx);
	if  ($rt){
	}else{
		COM_errorLog("error xml_parse ".$urlrss ,1);
	}
	xml_parser_free($xml_parser);

	return $values;
}

function assist_returnStaticpage(
    $page=''
    , $mode=''
    , $comment_order = 'ASC'
    , $comment_mode = 'nested'
    , $msg = 0
)
// +---------------------------------------------------------------------------+
// | 機能  静的ページを返す                                                    |
// | 書式 assist_returnStaticpage ($id ,"autotag")
// +---------------------------------------------------------------------------+
// | 引数 $page         :静的ページID                                          |
// | 引数 $mode         :""                                                    |
// |                    :print                                                 |
// |                    :sutotag                                               |
// | 引数 $comment_order:"ASC"                                                 |
// | 引数 $comment_mode :"nested"                                              |
// | 引数 $msg          :0                                                     |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:静的ページの内容                                               |
// +---------------------------------------------------------------------------+
// 2010/03/19 modify SP_returnStaticpage (gl1.6.1)
{
    global $_TABLES;
    global $_PLUGINS;

    $retval = '';

    if (!in_array('staticpages', $_PLUGINS)) {
        return $retval;
    }
    if ($page==="") {
        return $retval;
    }

    $sql = "SELECT sp_php, sp_content FROM {$_TABLES['staticpage']} "
         . "WHERE (sp_id = '" . addslashes($page) . "') "
         . "AND " . SP_getPerms();
    $result = DB_query($sql);

    if (DB_error() OR (DB_numRows($result) == 0)) {

        return $retval;
    } else {
        $A = DB_fetchArray($result);

        if ($mode == 'print') {
            $retval = SP_printPage($page, $A);
        } else if ($mode =='autotag') {
            $retval = SP_render_content(stripslashes($A['sp_content']), $A['sp_php']);
        } else {
            $retval = SP_displayPage($page, $retval, $comment_order, $comment_mode, $msg);
        }

        // increment hit counter for page
        DB_query("UPDATE {$_TABLES['staticpage']} SET sp_hits = sp_hits + 1 WHERE sp_id = '$page'");

    }

    return $retval;
}

function assist_returnUser(
    $item
    , $uid=0
    ,$width=0
    ,$height=0
)

// +---------------------------------------------------------------------------+
// | 機能 ユーザ情報を返す                                                     |
// | 書式 assist_returnUser("homepage")                                        |
// +---------------------------------------------------------------------------+
// | 引数 $item         :項目名                                                |
// | 引数 $uid          :0 省略時　ログインユーザ                              |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:指定ユーザ、指定項目(個人情報)の内容                           |
// +---------------------------------------------------------------------------+
// 20110117
{
    global $_CONF;
    global $_TABLES;
    global $_USER;

    global $LANG_ASSIST;

    if ($uid==0) {
        $uid=$_USER['uid'];
    }
    $login_uid=$_USER['uid'];


    $rt="";
    if ($uid<>0){

        switch($item) {
            //-----
            case 'fullname':
            case 'username':
                $rt = DB_getItem(
                    $_TABLES['users']." AS t1 "
                    ,$item
                    ,"t1.uid = ".$uid
                );

                break;

            case 'sig':
            case 'homepage':
                $w = DB_getItem(
                    $_TABLES['users']." AS t1 "
                    ,$item
                    ,"t1.uid = ".$uid
                );
                $rt=assist_autolink($w);
                break;
            case 'about':
            case 'pgpkey':
            case 'location':
                $rt = DB_getItem(
                    $_TABLES['userinfo']." AS t1 "
                    ,$item
                    ,"t1.uid = ".$uid
                );
                break;
            case 'photo':
                $photo = DB_getItem(
                    $_TABLES['users']." AS t1 "
                    ,$item
                    ,"t1.uid = ".$uid
                );
                $w="";
                if ($photo<>""){
                    $w="<img src=\"".$_CONF['site_url']."/images/userphotos/".$photo."\"";
                    if ($width<>0){
                        $w.=" width=\"".$width." ";
                    }
                    if ($height<>0){
                        $w.=" height=\"".$height." ";
                    }
                    $w.=" class=\"userphoto\">";
                }
                $rt=$w;
                break;

        }
    }
    return $rt;
}

//==============================================================================

function assist_nl2br (
    $text
)
// +---------------------------------------------------------------------------+
// | 機能 改行をBRに変換                                                       |
// | 書式 assist_nl2br ($text)                                                 |
// +---------------------------------------------------------------------------+
// | 引数 $text         :文字列                                                |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:改行をBRに変換後の文字列                                       |
// +---------------------------------------------------------------------------+
// 2010/08/03 copy from  "hiroron - 改行をBRに変換"
{
    return PLG_replacetags ( str_replace(array("\r\n","\r","\n"), '<br'.XHTML.'>', $text) );

}
// +---------------------------------------------------------------------------+
// | 機能 urlをリンク変換                                                      |
// | 書式 assist_autolink($text)                                               |
// +---------------------------------------------------------------------------+
// | 引数 $text         :文字列                                                |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:改行をBRに変換後の文字列                                       |
// +---------------------------------------------------------------------------+
// 2010/08/03 copy from  "hiroron - 自動リンクを
function assist_autolink (
    $text
) {
    $patterns = array("/(https?|ftp)(:\/\/[[:alnum:]\+\$\;\?\.%,!#~*\/:@&=_-]+)/i");
    $replacements = array("<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>");
    return preg_replace($patterns, $replacements, $text);
}

// +---------------------------------------------------------------------------+
// | 機能  テンプレートフォルダパスを取得
// | 書式 databox_templatePath('data','default','databox')
// +---------------------------------------------------------------------------+
// | 引数 $kind:'data' 'category' ''newlist' 'admin'
// | 引数 $template:  'default'
// | 引数 $pi_name: 'databox' 'userbox' 'formbox'
// +---------------------------------------------------------------------------+
// | 戻値 :テンプレートフォルダパス
// +---------------------------------------------------------------------------+
// 20101227 copy from functions_databox.php
function assist_templatePath (
    $kind = 'data'
    ,$template ='default'
    ,$pi_name= 'assit'
    )
{

    global $_CONF;

    if (is_null($template) OR ($template==="")){
        $template="default";
    }

    if (is_null($pi_name)){
        $pi_name="databox";
    }

    $box_conf="_".strtoupper($pi_name)."_CONF";
    global $$box_conf;
    $box_conf=$$box_conf;


    if ($kind==="admin"){
        $conf_templates=$box_conf["templates_admin"];
    }else{
        $conf_templates=$box_conf["templates"];
    }



    //"standard";//標準テンプレートを使用する
    //"custom";//カスタムテンプレートを使用する
    //"theme";//テーマテンプレートを使用する
    if  ($conf_templates==="theme"){

        $tmplfld=$_CONF['path_layout'] .$box_conf['themespath'].$kind;
        if ($kind<>"admin"){

            $tmplfld.="/".$template;
        }

        if (is_dir($tmplfld)) {

        }else{

            $tmplfld=$_CONF['path'] .'plugins/'.$pi_name.'/templates/'.$kind;
            if ($kind<>"admin"){
                $tmplfld.="/default";
            }
        }

    }else if  ($conf_templates==="custom"){

        $tmplfld=$_CONF['path'] .'plugins/'.$pi_name.'/custom/templates/'.$kind;

        if ($kind<>"admin"){

            $tmplfld.="/".$template;

        }

        if (is_dir($tmplfld)){

        }else{

            $tmplfld=$_CONF['path'] .'plugins/'.$pi_name.'/templates/'.$kind;
            if ($kind<>"admin"){

                $tmplfld.="/default";

            }
        }
    }else{

        $tmplfld=$_CONF['path'] .'plugins/'.$pi_name.'/templates/'.$kind;
        if ($kind<>"admin"){

            $tmplfld.="/default";
        }

    }


    return $tmplfld;
}

// +---------------------------------------------------------------------------+
// | 機能  ラジヲボタン作成                                                    |
// | 書式 assit_getyesno
// |                ($lang_noyes,"afield[".$fid."]",$fvalue,$brx ,$disabled);
// +---------------------------------------------------------------------------+
// | 引数 $selected:選択値　省略時                                             |
// +---------------------------------------------------------------------------+
// | 戻値 nomal:ラジヲボタン                                                   |
// +---------------------------------------------------------------------------+
function assist_getradiolist(
    $ary
    ,$fld
    ,$selected = 0
    ,$br=""
    ,$disabled=""
)
{
    $retval = '';

    foreach( $ary as $id => $value ){

        //echo "ary".$i."=".$ary[$i]."<br>";
        $retval .= "<input type='radio' NAME='$fld' $disabled VALUE='$id'";
        if( $id == $selected ){
                $retval .= " CHECKED=CHECKED ";
        }

        $retval .= ">$ary[$id]&nbsp".$br;
    }

    return $retval;
}

// +---------------------------------------------------------------------------+
// | 機能  ソーシャルボタン・ボックスのHTMlを返す
// | 書式 assist_btn($type,$thtml)
// | 		assist_btn('fblikebtn');
// | 		assist_btn('evclip');
// | 		assist_btn('fbcomment');
// | 		assist_btn('tweet');
// +---------------------------------------------------------------------------+
// | 引数 $type         :ボタンタイプ　省略値fblikebtn　type.thtml
// | 引数 $thtml        :テンプレートフォルダ名　省略時config値に準拠
// | 					 このフォルダは外部からアクセスできないようにすること
// +---------------------------------------------------------------------------+
// | 戻値 nomal:ソーシャルボタン・ボックスのHTMl
// +---------------------------------------------------------------------------+
// 2011/12/26
function assist_btn(
	$type =""
	,$thtml=""
)
{
	global $_CONF;
	
	$pi_name="assist";
	
	if ($type==""){
		$type="fblikebtn";
	}
	
	$retval = '';

    //テンプレートフォルダの設定
    $tmplfld=assist_templatePath('btn',$thtml,$pi_name);
	
	if (file_exists ($tmplfld."/".$type.".thtml")) {
	
		$btn = new Template($tmplfld);

		$btn->set_file (array (
			'btn' => $type.'.thtml',
			));
	
		$currenturl = COM_getCurrentURL();
		$appid=$_CONF['facebook_consumer_key'];
	
		$btn->set_var ('currenturl', $currenturl);
		$btn->set_var ('appid', $appid);

		$btn->parse ('output', 'btn');
		$retval .= $btn->finish ($btn->get_var ('output'));
	}
	
	return $retval;

}

//テスト中----->
function assist_loginform(
	$mode
	,$logoutform=""
)
{
	global $LANG_ASSIST;
	global $LANG01;
	
	$return ="";
	
	if (strtoupper($logoutform)==="YES"){
		$logoutform=true;
	}
	
	if( COM_isAnonUser() ) {
		if ($mode==="facebook"){
			$return = assist_loginform_oauth($mode);
		}
	}else{
		if ($logoutform){
			$url = $_CONF['site_url'] . '/users.php?mode=logout';
            $url = COM_buildUrl( $url );
            $link= COM_createLink($LANG01[19], $url);
			$return = $link;
		}
	}
	return $return;
}

//
function assist_loginform_oauth(
	$service
)
{
    global $_CONF;
	
	$return="";
	
	if ($_CONF[$service.'_login']){
        $login = COM_newTemplate($_CONF['path_layout']);
		$login->set_file('oauth_login', 'loginform_oauth.thtml');
		$login->set_var('oauth_service', $service);
		// for sign in image
		$login->set_var('oauth_sign_in_image', $_CONF['site_url'] 
			. '/images/login-with-' . $service . '.png');
		$login->set_var('oauth_sign_in_image_style', '');
		$login->parse('output', 'oauth_login');
	
		$return= $login->finish($login->get_var('output'));
	}
	
	return $return;

}


//テスト中<-----

function assist_usercount_location ()
//location 別ユーザ数
{
    global $_TABLES;

    $rt="";

    $sql="SELECT t1.location ,count(t1.uid) AS num  ";
    $sql.=" FROM {$_TABLES['userinfo']} AS t1";
    $sql.=" WHERE t1.location<>''";
    $sql.=" GROUP BY location ";
    $sql.=" ORDER BY num DESC ";
    $result = DB_query ($sql);
    $numrows = DB_numRows ($result);

    if ($numrows > 0) {
        for ($i = 0; $i < $numrows; $i++) {
            $A = DB_fetchArray ($result);
             $rt.=$A['location'].":".$A['num']."<br />".LB;
        }
    }
    return $rt;
}

//etc
function assist_arg (
    $item
)
{
    global $_CONF;
    global $_PLUGINS;

    global $LANG_ASSIST;
    global  $topic;
    global  $page;

    $rt="";
    switch( $item ) {

        case 'topic_id' :
            $rt=$topic;
            break;
        case 'sp_id' :
            if (in_array('staticpages', $_PLUGINS)) {
                $pageurl = COM_getCurrentURL();
                $staticpage=$_CONF['site_url']."/staticpages/index.php";
                if( strpos($pageurl, staticpage) !== FALSE ){
                    $rt=$page;
                }
            }

            break;
    }


    return $rt;
}


function assist_truncate(
	$text =''
	, $maxlen =''
	, $filler = ''
	, $endchars = '' )
//文字を指定数で区切る
{
	if  ($text==""){
		$rt="";
	}else{
		if  (!is_numeric($maxlen)){
			$maxlen=100;
		}
		if  (!is_numeric($endchars)){
			$endchars=0;
		}
		$rt=COM_truncate( $text, $maxlen, $filler, $endchars );
	}
    return $rt;
}

?>
